package xin.lz1998.cq.plugin.customize;

import cn.lliiooll.HttpRequestUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import xin.lz1998.cq.constant.GroupInfoConstant;
import xin.lz1998.cq.constant.TitleConstant;
import xin.lz1998.cq.constant.VoteKickConstant;
import xin.lz1998.cq.event.message.CQGroupMessageEvent;
import xin.lz1998.cq.robot.CQPlugin;
import xin.lz1998.cq.robot.CoolQ;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;


/**
 * 投票踢人
 */
@Component
@Order(2)//插件执行顺序
public class VoteKickPlugin extends CQPlugin {


    private LinkedHashMap<Long, String> map=new LinkedHashMap();

    private String groupAdmin="";

    @Override
    public int onGroupMessage(CoolQ cq, CQGroupMessageEvent event) {
        String msg=event.getMessage().trim() ;
        long groupId = event.getGroupId();
        Long adminId = event.getUserId();
        if(TitleConstant.GROUPID==groupId&&msg.startsWith(VoteKickConstant.CAIDAN)){
            String info="";
            info+="====菜单功能说明====\n";
            info+="====暂有以下功能====\n";
            info+="1.禁言 2.头衔 3.成员信息 4.飞机票\n";
            info+="5.笑话 6.性知识 7.女优大全\n";
            info+="8.疫情\n";
            info+="输入#(开头)如：#禁言 @xx\n";
            info+="========结束========\n";
            cq.sendGroupMsg (groupId, info, true);
            return MESSAGE_IGNORE;
        }

        if(TitleConstant.GROUPID==groupId&&msg.startsWith(VoteKickConstant.XIAOHUA)){
            String info = outXiaoHua();
            cq.sendGroupMsg (groupId, info, true);
            info="";
            return MESSAGE_IGNORE;
        }

        if(TitleConstant.GROUPID==groupId&&msg.startsWith(VoteKickConstant.XINGZHISHI)){
            String info = outXingZhiShi();
            cq.sendGroupMsg (groupId, info, true);
            info="";
            return MESSAGE_IGNORE;
        }
        if(TitleConstant.GROUPID==groupId&&msg.startsWith(VoteKickConstant.NVYOUDAQUAN)){
            String info = outNvYou();
            cq.sendGroupMsg (groupId, info, true);
            info="";
            return MESSAGE_IGNORE;
        }

        if(TitleConstant.GROUPID==groupId&&msg.startsWith(VoteKickConstant.YIQING)){
            String s = HttpRequestUtils.doGet("https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5");
            String[] arr = msg.split(" ");
            SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            StringBuilder sb = new StringBuilder();
            if(arr.length==1) {
                JSONObject data = getTotalInfo(s);
                JSONObject addData = getAddInfo(s);
                JSONArray totalAddr = getTotalAddr(s);
                for (int i = 0; i < totalAddr.size(); i++) {
                    System.out.println(totalAddr.getJSONObject(i).getString("today"));
                }
                sb.append("全国确诊\t" + data.getString("confirm") + "较上日+\t" + addData.getString("confirm") + "\n");
                sb.append("疑似病例\t" + data.getString("suspect") + "新增+\t" + addData.getString("suspect") + "\n");
                sb.append("治愈人数\t" + data.getString("heal") + "较上日+\t" + addData.getString("heal") + "\n");
                sb.append("死亡人数\t" + data.getString("dead") + "较上日+\t" + addData.getString("dead") + "\n");
                sb.append("=======统计数据=======\n");
                sb.append("地区\t" + "新增确诊\t" + "累计确诊\t" + "治愈\t" + "死亡\n");
                for (int i = 0; i < totalAddr.size(); i++) {
                    sb.append(totalAddr.getJSONObject(i).getString("name") + "\t"
                            + totalAddr.getJSONObject(i).getJSONObject("today").getString("confirm") + "\t\t"
                            + totalAddr.getJSONObject(i).getJSONObject("total").getString("confirm") + "\t\t"
                            + totalAddr.getJSONObject(i).getJSONObject("total").getString("heal") + "\t\t"
                            + totalAddr.getJSONObject(i).getJSONObject("total").getString("dead") + "\t\t" + "\n");
                }
                sb.append("更新于最新时间" + format.format(new Date()));
                cq.sendGroupMsg(groupId, sb.toString(), true);
                s = "";
                return MESSAGE_IGNORE;
            }
            if(arr.length==2) {
                JSONArray jsonArray = getXiangXiAddr(s);
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONArray children = jsonArray.getJSONObject(i).getJSONArray("children");
                    sb.append("===="+jsonArray.getJSONObject(i).getString("name")+"详细数据====\n");
                    sb.append("地区\t" + "新增确诊\t" + "累计确诊\t" + "治愈\t" + "死亡\n");
                    for (int j = 0; j <children.size() ; j++) {
                        sb.append(children.getJSONObject(j).getString("name") + "\t"
                                + children.getJSONObject(j).getJSONObject("today").getString("confirm") + "\t\t"
                                + children.getJSONObject(j).getJSONObject("total").getString("confirm") + "\t\t"
                                + children.getJSONObject(j).getJSONObject("total").getString("heal")+ "\t\t"
                                + children.getJSONObject(j).getJSONObject("total").getString("dead") + "\t\t" + "\n");
                    }

                    sb.append("合计"+ "\t"
                            + jsonArray.getJSONObject(i).getJSONObject("today").getString("confirm") + "\t\t"
                            + jsonArray.getJSONObject(i).getJSONObject("total").getString("confirm") + "\t\t"
                            + jsonArray.getJSONObject(i).getJSONObject("total").getString("heal") + "\t\t"
                            + jsonArray.getJSONObject(i).getJSONObject("total").getString("dead") + "\t\t" + "\n");




                    sb.append("更新于最新时间" + format.format(new Date()));
                    sb.append("!!");
                }
                String[] addr = sb.toString().split("!!");
                for (String s1 : addr) {
                    if(s1.contains(arr[1])){
                        cq.sendGroupMsg(groupId, s1, true);
                        s = "";
                        return MESSAGE_IGNORE;
                    }
                }
                System.out.println(addr);
            }
            cq.sendGroupMsg(groupId, "用户"+adminId+GroupInfoConstant.INFO, true);
            s = "";
            return MESSAGE_IGNORE;
        }

        String[] arr = msg.split(" ");
        if(TitleConstant.GROUPID==groupId&&msg.startsWith(VoteKickConstant.HEARD)&&arr.length>1){
            if(arr.length==2) {
                String[] idArr = arr[1].split("=");
                Long user_id = Long.valueOf(idArr[1].substring(0, idArr[1].length() - 1));
                if(user_id==VoteKickConstant.QUNZHUID){
                    cq.sendGroupMsg(groupId, "用户"+adminId+"你是皮痒了?群主都敢飞，不想活了???", true);
                    return MESSAGE_IGNORE;
                }
                if (user_id==VoteKickConstant.ADMINID) {
                    cq.sendGroupMsg(groupId, "用户"+adminId+"请注意你的身份，搞毛线!", true);
                    return MESSAGE_IGNORE;
                } else {
                    if(groupAdmin.indexOf( adminId + ",")==-1) {
                        groupAdmin += adminId + ",";
                    }else{
                        if (map.containsKey(user_id)) {
                            for (Map.Entry<Long, String> entry : map.entrySet()) {
                                if(entry.getKey().equals(user_id)) {
                                    String  mapValue = entry.getValue();
                                    if(mapValue.indexOf(adminId + ",")!=-1){
                                        cq.sendGroupMsg(groupId, "您已投过票，当前投票无效!", true);
                                        return MESSAGE_IGNORE;
                                    }
                                }
                            }
                        }
                    }
                    if (map.containsKey(user_id)&&map.containsValue(adminId + ",")) {
                        cq.sendGroupMsg(groupId, "您已投过票，当前投票无效!", true);
                    } else {
                        if(!map.containsKey(user_id)){
                            groupAdmin=adminId + ",";
                            map.put(user_id, groupAdmin);
                        }else{
                            map.put(user_id, groupAdmin);
                        }
                        Long mapKey = null;
                        String mapValue = "";
                        for (Map.Entry<Long, String> entry : map.entrySet()) {
                            if(entry.getKey().equals(user_id)) {
                                mapKey = user_id;
                                mapValue = entry.getValue();
                            }
                        }
                        int length = mapValue.split(",").length;
                        System.out.println("map=====" + map);
                        if (length < 5) {
                            cq.sendGroupMsg(groupId, "将对" + mapKey + "进行飞机票处理!投票处决,当前【" + length + "/5 】", true);
                        } else {
                            map.remove(user_id);
                            cq.setGroupKick(groupId, user_id, true);
                            cq.sendGroupMsg(groupId, "用户" + mapKey + "已被送往太空，真是大快人心啊!", true);
                        }
                    }
                }
            }else{
                cq.sendGroupMsg(groupId, "用户"+adminId+GroupInfoConstant.INFO, true);
                return MESSAGE_IGNORE;
            }
        }
        return MESSAGE_IGNORE;
    }

    private JSONArray getTotalAddr(String s) {
        JSONObject jsonObject = JSON.parseObject(s);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray jsonArray = data.getJSONArray("areaTree").getJSONObject(0).getJSONArray("children");
        return jsonArray;
    }

    private JSONObject getTotalInfo(String s) {
        JSONObject jsonObject = JSON.parseObject(s);
        JSONObject data = jsonObject.getJSONObject("data").getJSONObject("chinaTotal");
        return data;
    }

    private JSONObject getAddInfo(String s) {
        JSONObject jsonObject = JSON.parseObject(s);
        JSONObject data = jsonObject.getJSONObject("data").getJSONObject("chinaAdd");
        return data;
    }

    private static String outNvYou() {
        String filePath =  System.getProperty("user.dir");
        filePath += "\\nvyoudaquan.txt";
        String sfr = getSfr(filePath);
        String[] split = sfr.split("!!");
        Random random=new Random();
        int i=random.nextInt(split.length);
        return split[i];
    }

    private static String outXingZhiShi() {
        String filePath =  System.getProperty("user.dir");
        filePath += "\\xingzhishi.txt";
        String sfr = getSfr(filePath);
        String[] split = sfr.split("!!");
        Random random=new Random();
        int i=random.nextInt(split.length);
        return split[i];
    }

    public static void main(String[] args) {
        String filePath =  System.getProperty("user.dir");
        filePath += "\\nvyoudaquan.txt";
        String sfr = getSfr(filePath);
        String[] split = sfr.split("!!");
        Random random=new Random();
        int i=random.nextInt(split.length);
        System.out.println(split[i]);
    }

   /* public static void main(String[] args) {

        String s = HttpRequestUtils.doGet("https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5");
        JSONObject jsonObject = JSON.parseObject(s);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray jsonArray = data.getJSONArray("areaTree").getJSONObject(0).getJSONArray("children");
        for (int i = 0; i < jsonArray.size(); i++) {
            //System.out.println(jsonArray.getJSONObject(i).getString("name"));
            JSONArray children = jsonArray.getJSONObject(i).getJSONArray("children");
            //  System.out.println(jsonArray.getJSONObject(i).getString("name"));

            System.out.println( jsonArray.getJSONObject(i).getJSONObject("today").getString("confirm"));
            for (int j = 0; j <children.size() ; j++) {
             *//*   System.out.println(children.getJSONObject(j).getString("name")+"\t\t"+children.getJSONObject(j).getJSONObject("today").getString("confirm")
                        +"\t\t"+children.getJSONObject(j).getJSONObject("total").getString("confirm")+"\t\t"+children.getJSONObject(j).getJSONObject("total").getString("heal")
                        +"\t\t"+children.getJSONObject(j).getJSONObject("total").getString("dead"));*//*


            }
            System.out.println("========================");
        }
        //  System.out.println(data.getJSONArray("areaTree").getJSONObject(0).getJSONArray("children").getJSONObject(0).getString("name"));
    }*/

    private JSONArray getXiangXiAddr(String s) {
        JSONObject jsonObject = JSON.parseObject(s);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray jsonArray = data.getJSONArray("areaTree").getJSONObject(0).getJSONArray("children");
        return jsonArray;
    }

    private static String outXiaoHua(){
        String filePath =  System.getProperty("user.dir");
        filePath += "\\xiaohua.txt";
        String sfr = getSfr(filePath);
        String[] split = sfr.split("!!");
        Random random=new Random();
        int i=random.nextInt(split.length);
        return split[i];
    }

    private static String getSfr(String filePath) {
        BufferedReader reader = null;
        StringBuilder result = new StringBuilder();
        String temp = null;
        try {
            reader=new BufferedReader(new FileReader(filePath));
            try {
                while((temp=reader.readLine())!=null) {
                    result.append(System.lineSeparator()+temp);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        return result.toString();
    }

}
